﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.IO;

namespace GJLib
{
    public class JachiConvert
    {

        
        public void processYear(int year)
        {
           String fileName = @"D:\project\五術\data\jachi\"+year+".json";
           Dictionary<String, JObject> jachis1 = JachiHelper.LoadJachiOfYear(year-1);
           Dictionary<String, JObject> jachis2=JachiHelper.LoadJachiOfYear(year);
           //都加入~
           var a =(from o in jachis1.Values orderby o["time"].Value<DateTime>() select o).ToList<JObject>();
           var b =(from o in jachis2.Values orderby o["time"].Value<DateTime>() select o).ToList<JObject>();
           a.AddRange(b);
           List<Dictionary<String, Object>> rr = new List<Dictionary<string, object>>();
           foreach (var item in a)
           {
               Dictionary<String, Object> rec = new Dictionary<string, object>();
               DateTime dt=item["time"].Value<DateTime>();
               var daydata=GanJiHelper.fetchDaydata(dt.Year, dt.Month, dt.Day);
               if (daydata == null)
                   continue;
               List<String> gj = GanJiHelper.convertToList<String>(daydata["gj"]);
              //item[""].Value<>();
               rec.Add("name", item["name"].Value<String>());
               rec.Add("type", item["type"].Value<String>());
               rec.Add("id", item["id"].Value<String>());               
               rec.Add("time", item["time"].Value<DateTime>());
               int sz = gj.Count;
               for (int i = 0; i < sz; i++)
               {
                   gj[i]=GJR.checkGJ(gj[i]);
               }
               
               String monJ = gj[1].Substring(1, 1);
               String mg;
               if (rec["type"].Equals("氣"))
               {
                   mg=GJR.六合(monJ);
               }
               else
               {
                   mg=GJR.六合(GJR.prevJi(monJ));
               }
               rec.Add("mg", mg);
               rec.Add("gj", gj);
               rr.Add(rec);
               
           }
           if (rr.Count > 24)
           {
               int sz=rr.Count-27;
               rr.RemoveRange(0,sz );
           }
            String content=JsonConvert.SerializeObject(rr,Formatting.Indented);
            fileName = @"D:\project\五術\web\data\jachi\" + year + ".json";
            File.WriteAllText(fileName, content, Encoding.UTF8);

        }
    }
}
